//
//  GHUtils.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "TMUtils.h"

@implementation TMUtils
+ (void)netWorkMonitorinCanUseWithHandle:(void (^)())handle{
    
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
            if (handle) {
                handle();
            }
        }
    }];
    [manager startMonitoring];
}
+ (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    sectionHeader.backgroundColor = [UIColor whiteColor];
    STButton * line = [[STButton alloc] initWithFrame:CGRectMake(15, 0, 4, 22)
                                                title:nil
                                           titleColor:nil
                                            titleFont:14
                                         cornerRadius:0
                                      backgroundColor:KL_BlueBackGroundColor
                                      backgroundImage:nil
                                                image:nil];
    line.centerY = 22;
    [sectionHeader addSubview:line];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(line.right + 10, 0, 200, 44)
                                                     text:title
                                                textColor:KL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    
    [sectionHeader addSubview:titleLable];
    return sectionHeader;
}
+ (UINavigationBar *)navigationBar{
    UINavigationController* nav = (id)[UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers.firstObject;
    return nav.navigationBar;
}
+  (UITabBar *)tabbar{
    UITabBarController* tab = (id)[UIApplication sharedApplication].keyWindow.rootViewController;;
    return tab.tabBar;
}

#pragma mark --支付
+ (void)sendWechatPayWithOrder:(NSString *)orderID{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    // [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Weipay/weixin_pay"];
    if (orderID.length) {
        [dic setObject:orderID forKey:@"order"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                                 //NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 PayReq *request = [PayReq new];
                                                 request.nonceStr = dataDic[@"noncestr"];
                                                 request.package = dataDic[@"package"];
                                                 request.partnerId = dataDic[@"partnerid"];
                                                 request.prepayId = dataDic[@"prepayid"];
                                                 request.sign = dataDic[@"sign"];
                                                 request.timeStamp = [dataDic[@"timestamp"] intValue];
                                                 [WXApi sendReq:request];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
+ (void)sendAliPayWithOrder:(NSString *)orderID{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Online/signature"];
    //    url = @"http://39.104.78.211/pugai/alipay/signatures_url.php";
    if (orderID.length) {
        [dic setObject:orderID forKey:@"order"];
    }
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSString * alipayStr =  responseObject[@"data"];
                                                 [[AlipaySDK defaultService] payOrder:alipayStr fromScheme:@"blanket" callback:^(NSDictionary *resultDic) {
                                                     NSLog(@"reslut = %@",resultDic);
                                                     //回调在appdelegate 中
                                                 }];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}

+ (void)uploadImage:(UIImage*)image handle:(void(^)(NSString * url))handle{

    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/item_upload"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    NSData * data =  UIImageJPEGRepresentation(image, 0.5);
    NSString *dataStr = [data base64EncodedStringWithOptions:0];
    [dic setObject:dataStr forKey:@"img"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {

                                                 NSString * dataStr =  responseObject[@"data"];
                                                 if (handle) {
                                                     handle(dataStr);
                                                 }
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {

                                                 [SVProgressHUD dismiss];

                                             }];
}
@end
